package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.StoreGoods;
import com.xmy.cultivate.entity.StoreGoodsConsumeLog;
import com.xmy.cultivate.entity.Student;
import com.xmy.cultivate.entity.views.MathChaPlan;
import com.xmy.cultivate.mapper.StoreGoodsMapper;
import com.xmy.cultivate.service.IStoreGoodsConsumeLogService;
import com.xmy.cultivate.service.IStoreGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 物次库存 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-02
 */
@Service
public class StoreGoodsServiceImpl extends ServiceImpl<StoreGoodsMapper, StoreGoods> implements IStoreGoodsService {

    @Autowired
    StoreGoodsMapper storeGoodsMapper;

    @Autowired
    IStoreGoodsConsumeLogService iStoreGoodsConsumeLogService;

    @Autowired
    IStudentService iStudentService;

    @Override
    public boolean useGoodsNum(MathChaPlan mathChaPlan){
        QueryWrapper<StoreGoods> storeGoodsQueryWrapper = new QueryWrapper<>();
        storeGoodsQueryWrapper.eq("year_part",mathChaPlan.getYearPart());
        storeGoodsQueryWrapper.eq("quarter_num",mathChaPlan.getQuarterNum());
        storeGoodsQueryWrapper.eq("school_id",mathChaPlan.getSchoolId());
        storeGoodsQueryWrapper.eq("class_type_id",mathChaPlan.getClassTypeId());
        storeGoodsQueryWrapper.eq("subjects_id",mathChaPlan.getSubjectsId());
        storeGoodsQueryWrapper.eq("year_class_id",mathChaPlan.getYearClassId());
        storeGoodsQueryWrapper.eq("lesson_num",mathChaPlan.getLessonNum());
        storeGoodsQueryWrapper.eq("goods_type_id",2);
        storeGoodsQueryWrapper.eq("lesson_type",3);
        storeGoodsQueryWrapper.last("limit 1");
        StoreGoods storeGoods = this.getOne(storeGoodsQueryWrapper);

        QueryWrapper<StoreGoodsConsumeLog> storeGoodsConsumeLogQueryWrapper = new QueryWrapper<>();
        storeGoodsConsumeLogQueryWrapper.eq("consume_id",mathChaPlan.getId());
        storeGoodsConsumeLogQueryWrapper.last("limit 1");
        StoreGoodsConsumeLog storeGoodsConsumeLog = iStoreGoodsConsumeLogService.getOne(storeGoodsConsumeLogQueryWrapper);
        Long storeGoodsId = null;
        if (storeGoods!=null){
            storeGoodsId = storeGoods.getId();
            if (storeGoodsConsumeLog == null){
                Integer goodsCount = storeGoods.getGoodsCount();
                if (goodsCount>0){
                    goodsCount--;
                    storeGoods.setGoodsCount(goodsCount);
                    this.updateById(storeGoods);
                    this.addConsumeLog(mathChaPlan,1,storeGoodsId);
                }else {
                    this.addConsumeLog(mathChaPlan,2,storeGoodsId);
                }
            }
        }else {
            if (storeGoodsConsumeLog == null){
                this.addConsumeLog(mathChaPlan,2,storeGoodsId);
            }
        }
        return true;
    }

    public boolean addConsumeLog(MathChaPlan mathChaPlan,Integer status,Long storeGoodsId){
        StoreGoodsConsumeLog storeGoodsConsumeLog = new StoreGoodsConsumeLog();
        storeGoodsConsumeLog.setYearPart(mathChaPlan.getYearPart());
        storeGoodsConsumeLog.setQuarterNum(mathChaPlan.getQuarterNum());
        storeGoodsConsumeLog.setConsumeId(mathChaPlan.getId());
        storeGoodsConsumeLog.setStoreGoodsId(storeGoodsId);
        storeGoodsConsumeLog.setStudentId(mathChaPlan.getStudentId());
        storeGoodsConsumeLog.setSubjectsId(mathChaPlan.getSubjectsId());
        Student student = iStudentService.getById(mathChaPlan.getStudentId());
        String content;
        if (status.equals(1)){
            content = student.getName()+"第"+mathChaPlan.getLessonNum()+"次课,消费了1条库存";
        }else {
            content = student.getName()+"第"+mathChaPlan.getLessonNum()+"次课,消费了失败，库存不存在";
        }
        storeGoodsConsumeLog.setContent(content);
        storeGoodsConsumeLog.setStatus(status);
        iStoreGoodsConsumeLogService.save(storeGoodsConsumeLog);
        return true;
    }
}
